//
//  StoreDetailTopToolView.m
//  much
//
//  Created by weijunjie on 2019/1/9.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "StoreDetailTopToolView.h"

#define kPerWidth (ScreenWidth - 30 - 80) / 2.0

#define kBottomLineW [@"商品" boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(17)} context:nil].size.width
#define kOriginLineX (kPerWidth - kBottomLineW)
@interface StoreDetailTopToolView ()

/** 商品 */
@property (nonatomic, strong) UILabel *goodsLabel;
/** 详情 */
@property (nonatomic, strong) UILabel *detailWebLabel;
/** 底部线条 */
@property (nonatomic, strong) UIView *bottomLineView;
/** 商品详情,滚动到商品详情的时候显示 */
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation StoreDetailTopToolView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.goodsLabel.textColor = Color175030;
        self.detailWebLabel.textColor = Color34;
        self.bottomLineView.backgroundColor = Color175030;
    }
    return self;
}

- (void)setSlidToBottom:(BOOL)slidToBottom {
    _slidToBottom = slidToBottom;
    self.userInteractionEnabled = !slidToBottom;
    if (slidToBottom) {
        self.goodsLabel.hidden = self.detailWebLabel.hidden = self.bottomLineView.hidden = YES;
        self.goodsLabel.text = self.detailWebLabel.text = nil;
        self.titleLabel.hidden = NO;
        self.titleLabel.text = @"商品详情";
    } else {
        
        self.goodsLabel.hidden = self.detailWebLabel.hidden = self.bottomLineView.hidden = NO;
        self.goodsLabel.text = @"商品";
        self.detailWebLabel.text = @"详情";
        self.titleLabel.hidden = YES;
        self.titleLabel.text = nil;
    }
}

- (UILabel *)goodsLabel {
    if (!_goodsLabel) {
        _goodsLabel = [self createLabelWithTitle:@"商品" type:StoreDetailSwitchGoodsType];
        _goodsLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:_goodsLabel];
        [_goodsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.equalTo(self);
            make.height.mas_equalTo(44);
            make.width.mas_equalTo(kPerWidth);
        }];
    }
    return _goodsLabel;
}

- (UILabel *)detailWebLabel {
    if (!_detailWebLabel) {
        _detailWebLabel = [self createLabelWithTitle:@"详情" type:StoreDetailSwitchDetailWebType];
        _detailWebLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:_detailWebLabel];
        [_detailWebLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(self);
            make.height.mas_equalTo(44);
            make.width.mas_equalTo(kPerWidth);
        }];
    }
    return _detailWebLabel;
}

/** 统一创建label */
- (UILabel *)createLabelWithTitle:(NSString *)title
                             type:(StoreDetailSwitchType)type  {
    UILabel *label = [[UILabel alloc] init];
    label.tag = type;
    label.textColor = Color34;
    label.font = APPFONT(17);
    label.text = title;
    __weakConfigure
    [label tapUpWithBlock:^(UIView *v) {
        StoreDetailSwitchType type = v.tag;
        if (__weakSelf.switchActionBlock) {
            __weakSelf.switchActionBlock(type);
        }
    }];
    return label;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = Color34;
        _titleLabel.font = APPFONT(17);
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.bottom.equalTo(self);
            make.height.mas_equalTo(44);
        }];
    }
    return _titleLabel;
}

/** 底部线条 */
- (UIView *)bottomLineView {
    if (!_bottomLineView) {
        _bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(kOriginLineX, 0, kBottomLineW, 2)];
        _bottomLineView.backgroundColor = Color175030;
        [self addSubview:_bottomLineView];
    }
    return _bottomLineView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.bottomLineView.y = self.height - self.bottomLineView.height;
}


#pragma mark - event
- (void)moveLineWithOffsetX:(CGFloat)offSetX {
    if (offSetX < 0) {
        offSetX = 0;
    }
    
    if (offSetX >= ScreenWidth) {
        offSetX = ScreenWidth;
    }
    
    CGFloat moveX = kOriginLineX + (kBottomLineW + 30) * (offSetX / ScreenWidth);
    
    [self moveLine:moveX];
}

- (void)moveLine:(CGFloat)x {
    self.bottomLineView.x = x;
    if (x < (ScreenWidth - 80 - kBottomLineW) * 0.5) {
        self.goodsLabel.textColor = Color175030;
        self.detailWebLabel.textColor = Color34;
    } else {
        self.goodsLabel.textColor = Color34;
        self.detailWebLabel.textColor = Color175030;
    }
}

@end
